import * as api from '../../api/index';
import {
	Notification
} from 'element-ui';
const state = {
	data: [], //教练列表数据
	query: {}, //列表查询条件
	page: {
		total: 0
	}, //列表分页数据
	list_loading: false, //列表加载状态
}

const actions = {
	getListData({
		commit
	}, params) { //获取教练列表
		commit('update', {
			type: 'list_loading',
			data: true
		});
		api.coach.getList(params, res => {
			commit('update', {
				type: 'list_loading',
				data: false
			});
			if (res.status == 1) {
				commit('update', {
					type: 'query',
					data: params
				}); //更新查询数据
				commit('update', {
					type: 'data',
					data: res.data
				}); //更新列表数据
				commit('update', {
					type: 'page',
					data: res.page
				}); //更新分页数据
			} else if (res.message) Notification(res);
		});
	},
	delListData({
		commit
	}, params) {
		commit('delList', params);
	}
}

const mutations = {
	update(state, p) { //更新数据状态
		state[p.type] = p.data;
	},
	delList(state, params) {
		state.data.map((value, index) => {
			if (value.id.toString() == params) {
				state.data.splice(index, 1);
			}
		});
	},
	del(state, str) { //删除数据
		eval(str);
	}
}

export default {
	namespaced: true, //开启命名空间
	state,
	actions,
	mutations
}